package light.head.controller.mobile;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.mobile.MobileLeaveManageDAO;
import light.head.dao.oa.AuditStatusDAO;
import light.head.dao.oa.LeaveDAO;
import light.head.dao.oa.ProcessConfigDAO;
import light.head.dao.sys.MessageDAO;
import light.head.dao.sys.MessageTemplateDAO;
import light.head.dao.sys.OrganDAO;
import light.head.dao.sys.UserInfoDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.MD5Utils;
import light.head.model.AuditStatus;
import light.head.model.LeaveManage;
import light.head.model.ProcessConfig;
import light.head.model.UserInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 手机版 请假申请审核
 * @author 
 */
@Controller
@RequestMapping(Url.MOBILE_LEAVEA)
public class MobileLeaveController extends SupporterController {
	
	private LeaveDAO leaveDAO;
	private OrganDAO organDAO;
	private AuditStatusDAO asDAO;
	private MessageTemplateDAO mtDAO;
	private MessageDAO msgDAO;
	private UserInfoDAO userDAO;



	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		System.out.println("进入手机版的员工请假....");
		return new ModelAndView(Url.MOBILE_LEAVEA_INIT);
	}
	
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.HUSER, Parm.ST, Parm.END, Parm.UC, Parm.REASON);
		//Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if (leaveDAO.findByMap1(organDAO.getSonCodeList(cu_organCode(request)), cu_roleCode(request),map, pb) == RESULT.SUCCESS) {
			LeaveManage leave = null;
			for (Object o : pb.getResultList()) {
				leave = (LeaveManage) o;
				if (leave.getHead() == false){
					leave.setAuditStatusList(asDAO.findByBusinessMcRc(Sys.B_TWO, leave.getCode(), cu_roleCode(request)));
				}else{
					leave.setAuditStatusList(asDAO.findByBusinessMcRc(Sys.B_SIX, leave.getCode(), cu_roleCode(request)));
				}
				
			}
		}
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.MOBILE_LEAVEA_LIST);
	}
	

	@Transactional
	@RequestMapping(value = Url.EDITAGREE)
	public ModelAndView editAgree(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (isNotNull(code)) {
			LeaveManage leave = leaveDAO.get(Integer.parseInt(code));
			if (leave.getHead() == false){
				leave.setAuditStatusList(asDAO.findByBusinessMcRc(Sys.B_TWO, leave.getCode(), cu_roleCode(request)));
			}else{
				leave.setAuditStatusList(asDAO.findByBusinessMcRc(Sys.B_SIX, leave.getCode(), cu_roleCode(request)));
			}
			
			request.setAttribute(Parm.LEAVE, leave);
		}
		return new ModelAndView(Url.MOBILE_LEAVEA_DETAIL);
	}

	@Transactional
	@RequestMapping(value = Url.SAVEAGREE)
	public void saveAgree(LeaveManage leave, HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		LeaveManage re = leaveDAO.get(leave.getCode());
		Byte business ;
		if(re.getHead() == false){
			business = Sys.B_TWO;
		}else{
			business = Sys.B_SIX;
		}
		// 查询当前申请信息是否已被当前用户审核
		List<AuditStatus> asList =  asDAO.findByRcMcAuBusiness(cu_roleCode(request), re.getCode(), cu_code(request),business);
		if (asList == null || asList.size() > 0) {
			print(response, Msg.EXISTS);
			return;
		}
		// 1.判断是否有审核权限 查询流程配置表
		List<AuditStatus> auList = asDAO.findByRcMcBusiness(cu_roleCode(request), re.getCode(), business);
		if (auList == null || auList.size() < 0) {
			print(response, Msg.ERROR);
			return;
		}
		// 判断是否有上一级审核
		//Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		Integer ishave = asDAO.ishave(cu_roleCode(request), re.getCode(), business, organDAO.getSonCodeList(cu_organCode(request)));
		if (ishave !=null && ishave > 1) {
			// 2.判断上一级审核节点是否已经审核通过，查询流程配置表和审核状态表
			Byte beforeStatus = asDAO.checkBeforeByBusinessRc(business, cu_roleCode(request), organDAO.getSonCodeList(cu_organCode(request)), re.getCode());
			// 3.有审核权限，且上一级审核节点已经审核通过，进行保存本次审核状态
			if (beforeStatus == null || beforeStatus != Sys.B_ONE) {
				print(response, Msg.PARAMETER_ERROR);
				return;
			}
		}
		if (null != re.getCode()) {
			List<AuditStatus> asList1 = leaveDAO.findAuditStatusBusiness(re.getCode(), cu_roleCode(request), business);
			for (AuditStatus as : asList1) {
				as.setStatus(Sys.B_ONE);
				as.setAu(cu_code(request));
				as.setAt(new Date());
				result = asDAO.upd(as);
				// 查询是否有下一节点，有就修改下一节点审核状态为当前审核状态并发送消息通知下一审核人，没有就通知申请提交人
				List<AuditStatus> pcList = asDAO.findByOcPcMcBusiness(as.getPc() + 1, re.getCode(), business);
				if (null != pcList && pcList.size() > 0) {
					for(AuditStatus aud : pcList){
						aud.setBeforeStatus(Sys.B_ONE);
						result = asDAO.upd(aud);
						Integer rc1 = userDAO.findByOc(aud.getCu(), aud.getOc());
						//下一节点审核人角色和申请人角色相同则直接通过，不相等则直接通知下一审核人
						if (null != rc1 && rc1 == aud.getRc()){
							aud.setStatus(Sys.B_ONE);
							aud.setAt(new Date());
							aud.setAu(aud.getCu());
							// 查询是否有下一节点，有就修改下一节点审核状态为当前审核状态并发送消息通知下一审核人，没有就通知申请提交人
							List<AuditStatus> pc1List = asDAO.findByOcPcMcBusiness(aud.getPc() + 1, re.getCode(), business);
							if (null != pcList && pcList.size() > 0) {
								for(AuditStatus ad : pc1List){
									ad.setBeforeStatus(Sys.B_ONE);
									result = asDAO.upd(ad);
								} 
								//审核中
								re.setPass(Sys.B_FOUR);
								leaveDAO.upd(re);
								//消息通知审核人
								String name =userDAO.findCode(aud.getAu());
								Integer rc = asDAO.findByMcOcPc(cu_organCode(request), aud.getMc(), aud.getPc() + 1);	// 获取该机构具有审核申请信息的角色编号
								if(null == rc ){
									print(response, Msg.ERROR);
									return;
								}
								List<UserInfo> userList = userDAO.findByRoleRc(cu_organCode(request), rc);	// 获取有审核角色的所有用户
								List<Integer> rucs = new ArrayList<Integer>();
								for (UserInfo u : userList) {
									rucs.add(u.getCode());
								}
								String content = mtDAO.leaveFirstToTwo(re, re.getCuname(), cu_realName(request), aud.getCt());
								msgDAO.sendToUsers("【" + name + "】提交申请单待审核通知", content, as.getCu(), cu_organCode(request), rucs);
								print(response,Msg.SUCCESS);
							} else {
								//全部通过
								re.setPass(Sys.B_THREE);
								leaveDAO.upd(re);
								
								String content = mtDAO.throughToCu(re.getCt(), as.getAt());
								msgDAO.sendToUsers("【" + cu_realName(request) + "】审核通知", content, cu_code(request), cu_organCode(request), re.getCu());
								print(response,Msg.SUCCESS);
							}
						
						} else {
							//审核中
							re.setPass(Sys.B_FOUR);
							leaveDAO.upd(re);
							 //消息通知审核人
							Integer rc = asDAO.findByMcOcPc(cu_organCode(request), as.getMc(), as.getPc() + 1);	// 获取该机构具有审核申请信息的角色编号
							if(null == rc ){
								print(response, Msg.ERROR);
								return;
							}
							List<UserInfo> userList = userDAO.findByRoleRc(cu_organCode(request), rc);	// 获取有审核角色的所有用户
							List<Integer> rucs = new ArrayList<Integer>();
							for (UserInfo u : userList) {
								rucs.add(u.getCode());
							}
							LeaveManage lm = leaveDAO.get(re.getCode());
							String content = mtDAO.leaveFirstToTwo(lm, lm.getCuname(), cu_realName(request), as.getAt());
							msgDAO.sendToUsers("【" + cu_realName(request) + "】提交申请单待审核通知", content, cu_code(request), cu_organCode(request), rucs);
							print(response,Msg.SUCCESS);
						}
						aud.setBeforeStatus(Sys.B_ONE);
						result = asDAO.upd(aud);
					}
				} else if (RESULT.SUCCESS == result) {
					//全部通过
					re.setPass(Sys.B_THREE);
					leaveDAO.upd(re);
					LeaveManage lm = leaveDAO.get(re.getCode());
					String content = mtDAO.throughToCu(re.getCt(), as.getAt());
					msgDAO.sendToUsers("【" + cu_realName(request) + "】审核通知", content, cu_code(request), cu_organCode(request), lm.getCu());
					print(response,Msg.SUCCESS);
				}
			}
		} 
	}
	
	
	@RequestMapping(Url.UPDATEDESIGNATE_2)
	public void updateDesignate2(String code, String remark,HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 查询当前申请信息是否已被当前用户审核
		LeaveManage leave = leaveDAO.get(Integer.parseInt(code));
		Byte business ;
		if(leave.getHead() == false){
			business = Sys.B_TWO;
		}else{
			business = Sys.B_SIX;
		}
		List<AuditStatus> asList =  asDAO.findByRcMcAuBusiness(cu_roleCode(request), Integer.parseInt(code), cu_code(request), business);
		if (asList == null || asList.size() > 0) {
			print(response, Msg.EXISTS);
			return;
		}
		// 1.判断是否有审核权限 查询流程配置表
		List<AuditStatus> auList = asDAO.findByRcMcBusiness(cu_roleCode(request), Integer.parseInt(code), business);
		if (auList == null || auList.size() < 0) {
			print(response, Msg.ERROR);
			return;
		}
		// 判断是否有上一级审核
		//Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		Integer ishave = asDAO.ishave(cu_roleCode(request), Integer.parseInt(code), business, organDAO.getSonCodeList(cu_organCode(request)));
		if (ishave !=null && ishave > 1) {
			// 2.判断上一级审核节点是否已经审核通过，查询流程配置表和审核状态表
			Byte status = asDAO.checkBeforeByBusinessRc(business, cu_roleCode(request), organDAO.getSonCodeList(cu_organCode(request)), Integer.parseInt(code));
			// 3.有审核权限，且上一级审核节点已经审核通过，进行保存本次审核状态
			if (status == null || status != Sys.B_ONE) {
				print(response, Msg.PARAMETER_ERROR);
				return;
			}
		}
		if (null != code && null != remark) {
			List<AuditStatus> asList1 = leaveDAO.findAuditStatusBusiness(Integer.parseInt(code), cu_roleCode(request),business);
			for (AuditStatus as : asList1) {
				as.setStatus(Sys.B_TWO);
				as.setAu(cu_code(request));
				as.setAt(new Date());
				as.setRemark(remark);
				if (RESULT.SUCCESS == asDAO.upd(as)) {
					String content = mtDAO.rejectToSubmitter(leave, cu_realName(request),as.getAt(), as.getRemark());
					msgDAO.sendToUsers("【" + cu_realName(request) + "】驳回了申请单", content, cu_code(request), cu_organCode(request), leave.getCu());
					print(response,Msg.SUCCESS);
				}
			}
		}else {
			print(response,Msg.PARAMETER_ERROR);
			return;
		}
	}


	@Autowired
	public void setLeaveDAO(LeaveDAO leaveDAO) {
		this.leaveDAO = leaveDAO;
	}

	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}

	@Autowired
	public void setAsDAO(AuditStatusDAO asDAO) {
		this.asDAO = asDAO;
	}
	@Autowired
	public void setMtDAO(MessageTemplateDAO mtDAO) {
		this.mtDAO = mtDAO;
	}
	@Autowired
	public void setMsgDAO(MessageDAO msgDAO) {
		this.msgDAO = msgDAO;
	}
	@Autowired
	public void setUserDAO(UserInfoDAO userDAO) {
		this.userDAO = userDAO;
	}
	
}
